<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">REGISTER</view>
			<view class="welcome">
				欢迎注册！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">用户名</text>
					<input type="text" v-model="username" placeholder="请输入用户名" maxlength="20"/>
				</view>
				<view class="input-item">
					<text class="tit">密码</text>
					<input type="text" v-model="password" placeholder="8-18位不含特殊字符的数字、字母组合" placeholder-class="input-empty" maxlength="20"
					 password />
				</view>
				<view class="input-item">
					<text class="tit">手机号</text>
					<input type="text" v-model="telephone" placeholder="请输入手机号" maxlength="11"/>
				</view>
				<view class="input-item verification-item">
					<text class="tit">验证码</text>
					<input type="text" v-model="authCode" placeholder="请输入验证码" maxlength="6" class="verification-input"/>
					<text class="get-code-btn" @click="getAuthCode" :disabled="codeDisabled">{{codeText}}</text>
				</view>
			</view>
			<button class="confirm-btn" @click="toRegister" :disabled="registering">注册</button>
		</view>
		<view class="login-section">
			已有账号?
			<text @click="toLogin">立即登录</text>
		</view>
	</view>
</template>

<script>
	import {
		memberRegister,
		getAuthCode
	} from '@/api/member.js';
	
	export default {
		data() {
			return {
				username: '',
				password: '',
				telephone: '',
				authCode: '',
				registering: false,
				codeDisabled: false,
				codeText: '获取验证码',
				countdown: 0
			}
		},
		onLoad() {
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			toLogin() {
				uni.navigateTo({url:'/pages/public/login'});
			},
			async getAuthCode() {
				if (!this.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				
				if (!/^1[3-9]\d{9}$/.test(this.telephone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				if (this.codeDisabled) {
					return;
				}
				
				try {
					const response = await getAuthCode({telephone: this.telephone});
					console.log('验证码响应数据:', response); // 调试用
					
					// 检查响应中是否包含验证码（开发/测试环境）
					if (response.data && typeof response.data === 'string' && response.data.length === 6) {
						console.log('找到验证码:', response.data); // 调试用
						uni.showToast({
							title: `验证码：${response.data}`,
							icon: 'none',
							duration: 10000
						});
					} else {
						console.log('未找到验证码，显示成功提示'); // 调试用
						uni.showToast({
							title: '验证码已发送',
							icon: 'success'
						});
					}
					
					this.startCountdown();
				} catch (error) {
					let errorMessage = '发送失败，请重试';
					console.log('验证码错误详情:', error); // 调试用
					
					// 尝试多种可能的错误信息路径
					if (error.response && error.response.data) {
						if (error.response.data.message) {
							errorMessage = error.response.data.message;
						} else if (error.response.data.data && error.response.data.data.message) {
							errorMessage = error.response.data.data.message;
						} else if (typeof error.response.data === 'string') {
							errorMessage = error.response.data;
						}
					} else if (error.data && error.data.message) {
						errorMessage = error.data.message;
					} else if (error.message) {
						errorMessage = error.message;
					}
					
					uni.showToast({
						title: errorMessage,
						icon: 'none'
					});
				}
			},
			startCountdown() {
				this.codeDisabled = true;
				this.countdown = 60;
				this.codeText = `${this.countdown}s后重发`;
				
				const timer = setInterval(() => {
					this.countdown--;
					this.codeText = `${this.countdown}s后重发`;
					
					if (this.countdown <= 0) {
						clearInterval(timer);
						this.codeDisabled = false;
						this.codeText = '获取验证码';
					}
				}, 1000);
			},
			async toRegister() {
				if (!this.username) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}
				
				if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				
				if (!this.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				
				if (!this.authCode) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				
				this.registering = true;
				
				try {
					await memberRegister({
						username: this.username,
						password: this.password,
						telephone: this.telephone,
						authCode: this.authCode
					});
					
					uni.showToast({
						title: '注册成功',
						icon: 'success'
					});
					
					setTimeout(() => {
						uni.navigateTo({url:'/pages/public/login'});
					}, 1500);
					
				} catch (error) {
					let errorMessage = '注册失败，请重试';
					console.log('注册错误详情:', error); // 调试用
					
					// 尝试多种可能的错误信息路径
					if (error.response && error.response.data) {
						if (error.response.data.message) {
							errorMessage = error.response.data.message;
						} else if (error.response.data.data && error.response.data.data.message) {
							errorMessage = error.response.data.data.message;
						} else if (typeof error.response.data === 'string') {
							errorMessage = error.response.data;
						}
					} else if (error.data && error.data.message) {
						errorMessage = error.data.message;
					} else if (error.message) {
						errorMessage = error.message;
					}
					
					uni.showToast({
						title: errorMessage,
						icon: 'none'
					});
				} finally {
					this.registering = false;
				}
			}
		},

	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.verification-item {
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		
		.verification-input {
			flex: 1;
			margin-right: 20upx;
		}
		
		.get-code-btn {
			font-size: $font-sm;
			color: $uni-color-primary;
			padding: 10upx 20upx;
			border: 1px solid $uni-color-primary;
			border-radius: 4px;
			white-space: nowrap;
			
			&:active {
				opacity: 0.7;
			}
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.login-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>
